/*
  Copyright © 2010  Vinícius dos Santos Oliveira

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#include "textelement.h"
using namespace Touche;
#include <QPainter>
#include <QTextDocument>
#include <QTextCursor>
#include <QSvgRenderer>

// class TextItem

static const QString null_string("Type here:");
static QSvgRenderer renderer(QString(":/gfx/svg/item_text.svg"));

TextItem::TextItem(QGraphicsItem *parent, QGraphicsScene *scene) :
        QGraphicsTextItem(null_string, parent, scene),
        m_document(QGraphicsTextItem::document())
{
    QGraphicsTextItem::setFont(QFont("purisa", 8/*, 25*/));
    QGraphicsTextItem::setTextWidth(120.);
}

void TextItem::keyPressEvent(QKeyEvent *event)
{
    QGraphicsTextItem::keyPressEvent(event);
    if(m_document->lineCount() > 6){
        //TODO: permitir que somente 6 linhas sejam inseridas
    }
    if(m_document->characterCount() > 120){
        QGraphicsTextItem::textCursor().deletePreviousChar();
    }
}

void TextItem::focusOutEvent(QFocusEvent *event)
{
    QGraphicsTextItem::focusOutEvent(event);
    QGraphicsTextItem::setTextInteractionFlags(Qt::NoTextInteraction);
    QGraphicsTextItem::setTextCursor(QTextCursor(m_document));
    emit finished();
}

void TextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//    painter->setPen(QPen(QBrush(QColor(0x8F, 0x85, 0, 0xC8)), Qt::SolidPattern));
//    painter->setBrush(QBrush(QColor(0xF3, 0xE9, 0x15, 0xFF), Qt::SolidPattern));
//    painter->drawRoundedRect(QRectF(0., 0., 160., 120.), 5., 5., Qt::RelativeSize);
    renderer.render(painter, QRectF(0., 0., 120., 120.));
    QGraphicsTextItem::paint(painter, option, widget);
}

QRectF TextItem::boundingRect() const
{
    return QRectF(0., 0., 120., 120.);
}

// CLASS TextElement

static const QString type("text");

TextElement::TextElement() :
        locked(false),
        m_item(new TextItem)
{
}

const QString &TextElement::type() const
{
    return ::type;
}

//Element::Options TextElement::flags()
//{
//    return EDITABLE_IN_STAGE;
//}

QRectF TextElement::item(QGraphicsItem *parentItem) const
{
    m_item->setParentItem(parentItem);
    return m_item->boundingRect();
}

QVector<QString> TextElement::menuOptions() const
{
    static const QVector<QString> defaultAction(1, "edit");
    if(locked){
        return QVector<QString>();
    }else{
        return defaultAction;
    }
}

void TextElement::onMenuActived(const QString &)
{
    //sabendo que a única opção é editar o menu, não precisamos fazer nenhum tratamento da string
    emit lockElement();
    m_item->setTextInteractionFlags(Qt::TextEditorInteraction);
    m_item->setFocus();
    if(m_null){
        m_item->document()->setPlainText(QString());
    }
    connect(m_item, SIGNAL(finished()), this, SLOT(onEditionFinished()));
}

void TextElement::onEditionFinished()
{
    disconnect(m_item, SIGNAL(finished()), this, SLOT(onEditionFinished()));
    QString edited(m_item->toPlainText());
    if(edited.isEmpty()){
        m_item->setPlainText("Type here:");

        buffer.clear();
        m_null = true;

        emit contentsChanged();
    }else if(edited != buffer){
        buffer = edited;
        m_null = false;

        emit contentsChanged();
    }
    emit lockElement(false);
}

void TextElement::onElementLocked(bool locked)
{
    if(this->locked != locked){
        this->locked = locked;
        if(locked){
            emit menuUpdated(QVector<QString>());
        }else{
            emit menuUpdated(QVector<QString>(1, "edit"));
        }
    }
}

QString TextElement::save() const
{
    return buffer;
}

void TextElement::load(const QString &xml, const QStringList &)
{
    if(!xml.isEmpty()){
        buffer = xml;
        m_null = false;
        m_item->document()->setPlainText(buffer);
    }else{
        buffer.clear();
        m_null = true;
        m_item->setPlainText("Type here:");
    }
}
